package myky.com.doctorapp.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import java.util.ArrayList;
import java.util.List;
import myky.com.doctorapp.R;

import myky.com.doctorapp.entity.DoctorEntity;
import myky.com.doctorapp.wight.SelfDialog;


/**
 * 选择对话框工具类
 * @author Mr.li
 */
public class DialogChoice {
    private static SelfDialog selfDialog;
    private static ProgressDialog pBar;
    private static ServiceConnection serviceConnection;
    private OnChooseListener onChooseListener;
    private OnChoosePhotoListener onChoosePhotoListener;
    private static int index_cancel = -1;

    public static void setDialog(final Context context, final String tilte, final int type) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                selfDialog = new SelfDialog(context);
                selfDialog.setTitle(tilte);
                selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        selfDialog.dismiss();
                        if (type == 1) {
                            ((Activity) context).finish();
                        }
                    }
                });
                selfDialog.show();
            }
        });
    }

    public static void setDialog(final Context context, final String tilte, final String message, final int type) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                selfDialog = new SelfDialog(context);
                selfDialog.setTitle(tilte);
                selfDialog.setMessage(message);
                selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        selfDialog.dismiss();
                        if (type == 1) {
                            ((Activity) context).finish();
                        }
                    }
                });
                selfDialog.show();
            }
        });


    }

    /**
     * 列表选择对话框
     */
    public static void setDialog_Choose(final Context context, String title, List<String> stringList, final OnChooseListener onChooseListener) {

        final Dialog dialog_window = new Dialog(context, R.style.ActionSheetDialogStyle);
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_choose_view, null);
        dialog_window.setContentView(inflate);
        Window window = dialog_window.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.y = 10;
        layoutParams.width = SystemUtil.getMetrics(context)[0] - 20;
        window.setAttributes(layoutParams);
        dialog_window.show();
        TextView cancel = inflate.findViewById(R.id.dialog_choose_cancel);
        TextView titles = inflate.findViewById(R.id.dialog_choose_title);
        LinearLayout linearLayout = inflate.findViewById(R.id.dialog_choose_recycler_lin);
        ViewGroup.LayoutParams lp = linearLayout.getLayoutParams();
        if (stringList.size() > 1) {
            //设置高度不超过两个item
            lp.height = (cancel.getLayoutParams().height ) * 2;
        }
        titles.setText(title);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog_window.dismiss();
            }
        });
        RecyclerView recyclerView = inflate.findViewById(R.id.dialog_choose_recycler);
        RecyclerUtils.setRecyclerParms(context, recyclerView, 1);
        recyclerView.setAdapter(new CommonAdapter<String>(context, R.layout.dialog_choose_recycler_item, stringList) {
            @Override
            protected void convert(ViewHolder holder, final String s, int position) {

                holder.setText(R.id.choose_recycler_item_tv, s);
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onChooseListener.onChooseCallback(s);
                        dialog_window.dismiss();
                    }
                });
            }
        });
    }

    /**
     * 图片选择对话框
     */
    public static void setDialog_ChoosePhoto(int intType, final Context context, int cameraLimit, final OnChoosePhotoListener onChoosePhotoListener) {

        final Dialog dialog_window = new Dialog(context, R.style.ActionSheetDialogStyle);
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_choose_photo_view, null);
        dialog_window.setContentView(inflate);
        Window window = dialog_window.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.y = 10;
        layoutParams.width = SystemUtil.getMetrics(context)[0] - 20;
        window.setAttributes(layoutParams);
        RelativeLayout mycase = inflate.findViewById(R.id.dialog_choose_photo_mycase);
        RelativeLayout take = inflate.findViewById(R.id.dialog_choose_photo_cancel_take);
        RelativeLayout photos = inflate.findViewById(R.id.dialog_choose_photos);
        RelativeLayout cancel = inflate.findViewById(R.id.dialog_choose_photo_cancel);
        TextView album = inflate.findViewById(R.id.dialog_choose_album);
        TextView camera = inflate.findViewById(R.id.dialog_choose_camera);
        if (cameraLimit == 0) {
            take.setEnabled(false);
            photos.setEnabled(false);
            album.setTextColor(context.getResources().getColor(R.color.gray_999999));
            camera.setTextColor(context.getResources().getColor(R.color.gray_999999));
        }
        //1我的病历选择框  0为远程、咨询、预约方式
        if (intType == 1) {
            mycase.setVisibility(View.GONE);
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_window.dismiss();
            }
        });

        List<RelativeLayout> layouts = new ArrayList<>();
        layouts.add(mycase);
        layouts.add(take);
        layouts.add(photos);

        for (int i = 0; i < layouts.size(); i++) {
            layouts.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.dialog_choose_photo_mycase:
                            onChoosePhotoListener.onChoosePhotoCallback(Constants.CASE_MEDICALRECORD);
                            break;
                        case R.id.dialog_choose_photo_cancel_take:
                            onChoosePhotoListener.onChoosePhotoCallback(Constants.TAKE_CAMERA);
                            break;
                        case R.id.dialog_choose_photos:
                            onChoosePhotoListener.onChoosePhotoCallback(Constants.TAKE_ALBUM);
                            break;
                            default:
                                break;
                    }
                    dialog_window.dismiss();
                }
            });
        }
        dialog_window.show();
    }

    //病历授权协议及隐私引导图
    public static void showJurisdiction_W(final Context context, @LayoutRes int resource) {
        final Dialog dialog_window = new Dialog(context, R.style.SheetDialogStyle);
        View inflate = LayoutInflater.from(context).inflate(resource, null);
        dialog_window.setContentView(inflate);
        Window window = dialog_window.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = SystemUtil.getMetrics(context)[0];
        window.setAttributes(layoutParams);
        dialog_window.show();
        TextView textView = inflate.findViewById(R.id.wdys_bisq_ydy_tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_window.dismiss();
            }
        });
    }
    //病历授权医生
    public static void showJurisdiction(final Context context, final DoctorEntity.PayLoadEntity beans, final JurisdictionOnListener onListener) {
        final Dialog dialog_window = new Dialog(context, R.style.ActionSheetDialogStyle);

        View inflate = LayoutInflater.from(context).inflate(R.layout.set_show_jurisdiction, null);

        dialog_window.setContentView(inflate);
        Window window = dialog_window.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = SystemUtil.getMetrics(context)[0];
        window.setAttributes(layoutParams);
        dialog_window.show();
        TextView cancel = inflate.findViewById(R.id.show_jurisdiction_cancel);
        SimpleDraweeView imageView = inflate.findViewById(R.id.show_jurisdiction_civ);
        TextView realName = inflate.findViewById(R.id.show_jurisdiction_realname);
        TextView level = inflate.findViewById(R.id.show_jurisdiction_level);
        TextView hospital = inflate.findViewById(R.id.show_jurisdiction_hospital);
        TextView department = inflate.findViewById(R.id.show_jurisdiction_department);
        Button button = inflate.findViewById(R.id.show_jurisdiction_btn);
        if (beans != null) {
            try {
                imageView.setImageURI(UriUtils.getUriInstance().getUri(beans.getHeadPhoto()));
                realName.setText(beans.getRealname());
                level.setText(context.getResources().getStringArray(R.array.administrationName_level)[beans.getAdministrationname()]);
                hospital.setText(beans.getHospitalname());
                department.setText(beans.getDepartmentname());

            } catch (Exception e) {
            }
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_window.dismiss();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_window.dismiss();
                onListener.jurisdictionCallback(true);
            }
        });
    }

    public interface OnChooseListener {
        void onChooseCallback(String date);
    }

    public interface OnChoosePhotoListener {
        void onChoosePhotoCallback(int onclick);
    }
    public interface JurisdictionOnListener {
        void jurisdictionCallback(boolean b);
    }
}
